﻿
using static ConsoleApplication3delegate.CreditCardDelegate;

namespace ConsoleApplication3delegate
{
    class DepositCard    //储蓄卡
    {
        public int amount;		//储蓄卡余额

        public void Display()   //获取储蓄卡的余额
        {
            Console.WriteLine("储蓄卡余额为：{0}", amount);
        }
        public void Account(int balance, int payday)    //储蓄卡进行还款
        {
            amount += balance;
            if(amount<0)
                Console.WriteLine("储蓄卡余额不足，还款失败");
            else
                Console.WriteLine("今天是本月的第{0}天，取款{1}元，储蓄卡余额为{2}元。",DateTime.Today.Day, balance, amount);
          
        }
    }

    class CreditCard   //信用卡
    {
        private int billamount;     //信用卡的还款金额
        private int repaymentday;   //信用卡的还款日期

        public CreditCard(int billamount, int repaymentday) //初始化信用卡
        {
            this.billamount = billamount;
            this.repaymentday = repaymentday;
        }

        public int getbillamount() { return billamount; }   //获取信用卡还款金额

        public int getrepaymentday() { return repaymentday; }   //获取信用卡还款日期

        public void Display() { Console.WriteLine("信用卡余额为：{0}", billamount); }  


    }

    class CreditCardDelegate                //事件与委托
    {
        public int billamount;              //还款金额
        public int repaymentday;            //还款日期
        public delegate void call(int billamount, int repaymentday);//创建委托去调用储蓄卡的还款方法
        public event call Paymoney;         //事件
        public void Notify()                
        {
            if (DateTime.Today.Day == repaymentday) { Paymoney(billamount, repaymentday); }//到还款日期时，触发事件，调用储蓄卡的还款方法
            else { Console.WriteLine("未到还款日"); }
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            DepositCard depositCard = new DepositCard();
            depositCard.amount = 10000;
            CreditCard creditCard1 = new CreditCard(-2000, 5);
            CreditCard creditCard2 = new CreditCard(-3000, 4);
            CreditCard creditCard3 = new CreditCard(-6000, 28);
            depositCard.Display(); Console.WriteLine("");
            List<CreditCard> Cards = new List<CreditCard>();
            Cards.Add(creditCard1); Cards.Add(creditCard2); Cards.Add(creditCard3);
            int i = 0;
            foreach (CreditCard card in Cards)
            {
                Console.WriteLine("信用卡开始执行委托还款。。。。。。");
                Console.Write("信用卡{0}, ",i++);
                card.Display();
                CreditCardDelegate credelegate ;           //创建一个对象并初始化
                credelegate = new CreditCardDelegate();
                credelegate.billamount = card.getbillamount();          //获取信用卡还款金额和还款日期
                credelegate.repaymentday = card.getrepaymentday();
                credelegate.Paymoney += new CreditCardDelegate.call(depositCard.Account);//订阅
                credelegate.Notify();
                Console.WriteLine("\n");
               
            }
            Console.ReadLine();
        }
    }
}